import 'package:flutter/material.dart';
import 'package:untitled/pages/login_widget/Home/login_home.dart';
import 'package:untitled/pages/login_widget/commponents/login_background.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:untitled/utils/Check.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:dio/dio.dart';
class LoginBody extends StatelessWidget {
  const LoginBody({
    Key? key,
  }) : super(key: key);
  // 账号和密码
  

  @override
  Widget build(BuildContext context) {
    String email = "";
    String password = "";
    Size size = MediaQuery.of(context).size;
    const login = true;
    return loginBackground(
      child: SingleChildScrollView(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
        children:[
          Text(
            "LOGIN",
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
          SizedBox(height:size.height * 0.05),
          SvgPicture.asset(
                  'assets/icons/login.svg',
                  height:size.height*0.35
          ),
          SizedBox(height:size.height * 0.05),
          Container(
            margin: EdgeInsets.symmetric(vertical: 10),
            padding:EdgeInsets.symmetric(vertical: 5,horizontal: 20),
            width: size.width *0.8,
            decoration: BoxDecoration(
              color: Colors.purple[400],
              borderRadius: BorderRadius.circular(29)
            ),
            child:TextField(
              decoration: InputDecoration(
                icon: Icon(
                  Icons.person,
                  color: Colors.purpleAccent[800],
                ),
                hintText: "Your Email",
                border: InputBorder.none
              ),
               onChanged: (value){
                email = value;
               },
            ),
                  
          ),
          SizedBox(height:size.height * 0.01),
          Container(
            margin: EdgeInsets.symmetric(vertical: 10),
            padding:EdgeInsets.symmetric(vertical: 5,horizontal: 20),
            width: size.width *0.8,
            decoration: BoxDecoration(
              color: Colors.purple[50],
              borderRadius: BorderRadius.circular(29)
            ),
            child:TextField(
              obscureText: true,
              decoration: InputDecoration(
                icon: Icon(
                  Icons.lock,
                  color: Colors.purpleAccent[800],
                ),
                suffixIcon: Icon(
                  Icons.visibility,
                  color: Colors.purpleAccent[800],
                ),
                hintText: "Password",
                border: InputBorder.none
              ),
              onChanged: (value){
                password = value;
              },
            ), 
          ),
          SizedBox(height: 10,),
          Container(
            width: size.width * 0.8,
            child: ClipRRect(
              borderRadius: BorderRadius.circular(29),
              child: FlatButton(
              padding:EdgeInsets.symmetric(vertical: 15,horizontal: 40),
              color: Colors.purple,
              onPressed: (){
                // 校验
                // if(!isEmail(email)){
                //   Fluttertoast.showToast(
                //     msg: "请保证邮箱正确!",
                //     gravity: ToastGravity.CENTER,
                //     textColor: Colors.grey
                //   );
                // }else
                 if(password == null || password==""){
                  Fluttertoast.showToast(
                    msg: "密码不能为空!",
                    gravity: ToastGravity.CENTER,
                    textColor: Colors.grey
                  );
                }else{
                  // 调试接口
                  loginFun(email,password,context);
                }
                
              }, 
              child: Text("LGIN",style: TextStyle(color: Colors.black),)
              ),
            ),
          ),
          SizedBox(height: 5,),
          Row(
            mainAxisAlignment:MainAxisAlignment.center,
            children: [
              Text(
                login ?"Don't have an Account ?":"Already have an Accout?",
                style: TextStyle(
                  color: Colors.purple),
              ),
              GestureDetector(
                onTap: (){},
                child: Text(
                  login ?"Sing Up" : "Sign In",
                  style: TextStyle(
                    color: Colors.purple,
                    fontWeight: FontWeight.bold),)),
            ],
          )
      
        ],
          ),
      ),);
  }

  // 登录页
  void loginFun(String email,String password,BuildContext context ) async{
    // 模拟接口
    // String loginURL = "locallhost"+'/user/login';
    // Dio dio = new Dio();
    // var response = await dio.post(
    //   loginURL,
    //   data: {'email':email,'password':password}
    //   );
    // print('Respone ${response.statusCode}');
    // print(response.data);
    // if (response.statusCode == 200) {//状态码200 请求是成功的
    //   Fluttertoast.showToast(
    //       msg: response.data,
    //       gravity: ToastGravity.CENTER,
    //       textColor: Colors.grey);
    //   if(response.data=="sucess"){
    //     Navigator.pop(context);
    //     Navigator.pop(context);
    //     Navigator.push(context,
    //         MaterialPageRoute(builder: (context) => home(email: email)));
    //   }
    // }
    // else{
    //   //showToast("服务器或网络错误!");
    // }
    Navigator.pop(context);
    Navigator.pop(context);
    Navigator.push(context,MaterialPageRoute(builder: (context) => home()));


  }
}

